package com.industry.framework.core.context;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.industry.framework.core.generic.GenericController;
import com.industry.framework.core.generic.GenericService;
import com.industry.framework.core.generic.chooser.CoreGenericChooser;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author nbZhuozh
 * @Date 2025-03-19  14:11
 **/
public class CoreApplicationContext {

    public static ApplicationContext applicationContext;

    public static List<String> acticveProfileList;

    //实现了core controller 的类
    public static Map<String , GenericController> GENERIC_CONTROLLER_MAP = new ConcurrentHashMap<>();
    //实现了core service 的类
    public static Map<String , GenericService> GENERIC_SERVICE_MAP = new ConcurrentHashMap<>();
    //实现了baseMapper 的类
    public static Map<String , BaseMapper> GENERIC_BASE_MAPPER = new ConcurrentHashMap<>();
    // 实现了chooser的类
    public static Map<String, CoreGenericChooser> GENERIC_BASE_CHOOSER = new ConcurrentHashMap<>();

    public static void setApplicationContext(ApplicationContext applicationContext) {
        CoreApplicationContext.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    public static Environment getEnvironment(){
        return applicationContext.getEnvironment();
    }

    public static Object getBean(Class<?> clazz){
        return applicationContext.getBean(clazz);
    }

    public static Object getBean(String name){
        return applicationContext.getBean(name);
    }
    //是否包含某个配置文件
    public static boolean isProfileActivated(String profile) {
        if(CollectionUtils.isEmpty(acticveProfileList)){
            acticveProfileList = Collections.unmodifiableList(Arrays.asList(getEnvironment().getActiveProfiles()));
        }
        return acticveProfileList.contains(profile);
    }

    public static Map<String, GenericController> getGenericControllerMap() { return GENERIC_CONTROLLER_MAP; }
    public static Map<String, GenericService> getGenericServiceMap() { return GENERIC_SERVICE_MAP; }
    public static Map<String, BaseMapper> getGenericBaseMapper() { return GENERIC_BASE_MAPPER; }
}
